﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Entity.Models;
using IntelligentParkingLot.Events;
using IServices;
using Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;

namespace IntelligentParkingLot.ViewModels;

public class MainViewModel : BindableBase
{
    private List<SysMenu> _originalMenus;
    private ObservableCollection<MenuItemModel> _menus = new();
    public readonly IRegionManager _regionManager;

    public string RegionName { get; set; } = RegionNameProvider.ContentRegion;

    public MainViewModel(
        IDialogService dialogService,
        IMenuService menuService,
        IRegionManager regionManager,
        IEventAggregator eventAggregator
    )
    {
        _regionManager = regionManager;
        // 打开登录窗口
        dialogService.ShowDialog(
            "LoginView",
            result =>
            {
                if (result.Result != ButtonResult.OK)
                    Environment.Exit(0);
            }
        );

        // 打开窗体的委托
        OpenViewCommand = new DelegateCommand<MenuItemModel>(OpenView);
        eventAggregator
            .GetEvent<RefreshMenuEvent>()
            .Subscribe(
                () =>
                {
                    LoadMenus(menuService);
                },
                true
            );
        // 加载菜单
        LoadMenus(menuService);
    }

    private void LoadMenus(IMenuService menuService)
    {
        // 初始化菜单
        Menus.Clear();
        _originalMenus = menuService.GetMenuList();
        FillMenus(Menus, 0);
    }

    /// <summary>
    ///   打开窗体
    /// </summary>
    /// <param name="targetView"></param>
    private void OpenView(MenuItemModel model)
    {
        // 如果是父节点就展开/关闭，如果是子节点就打开
        if (model.Children != null && model.Children.Any())
        {
            model.IsExpanded = !model.IsExpanded;
        }
        else if (!string.IsNullOrEmpty(model.TargetView))
        {
            _regionManager.Regions["ContentRegion"].RequestNavigate(model.TargetView);
        }
    }

    public ObservableCollection<MenuItemModel> Menus
    {
        get => _menus;
        set => SetProperty(ref _menus, value);
    }

    /// <summary>
    /// 填充菜单
    /// </summary>
    /// <param name="menuList"></param>
    /// <param name="parent_id"></param>
    private void FillMenus(ObservableCollection<MenuItemModel> menuList, int parent_id)
    {
        var sub = _originalMenus
            .Where(m => m.ParentId == parent_id)
            .OrderBy(o => o.MenuIndex)
            .ToList();

        if (sub.Any())
            foreach (var item in sub)
            {
                var model = new MenuItemModel
                {
                    MenuHeader = item.MenuHeader,
                    MenuIcon = item.MenuIcon,
                    TargetView = item.TargetView
                };
                menuList.Add(model);

                FillMenus(model.Children, item.Id);
            }
    }

    public DelegateCommand<MenuItemModel> OpenViewCommand { get; set; }
}
